require "autogen.NetworkMessageHandlerLibrary"

local luaproto=CS.Kylin.ResourceMgr
local pb = require "pb"
local protoString = luaproto.LoadPb("autogen/PB_Bag.pb")
pb.load(protoString)

PB_Bag = {}

function PB_Bag:RegisterAction(msgId, func)
    LDebug.Log('开始注册')
    LDebug.Log(func)
    if NetworkMessageHandler[msgId] then error('重复注册网络消息处理方法. ' .. '-' .. msgId) end
    NetworkMessageHandler[msgId] = func
end

------ Server 2 Client --------
PB_Bag.UseItemAllAck = 608
NetworkMessageDecoder[PB_Bag.UseItemAllAck] = function(msgId, data) 
    local handler = NetworkMessageHandler[msgId]
    if not handler then print('网络消息没有注册处理方法.' .. '-' .. msgId); return end

    LDebug.Log("开始decode 608 消息")

    local person = { -- 我们定义一个addressbook里的 Person 消息
        name = "Alice",
        id = 12345,
    }

    -- 序列化成二进制数据
    local data = assert(pb.encode("tutorial.Person", person))
    CS.UnityEngine.Debug.Log(#data);
 

    -- 从二进制数据解析出实际消息
    local msg = assert(pb.decode("tutorial.Person", data))
    handler[msgId](msg)
end

----- Client 2 Server --------
PB_Bag.GetAllItemReq = 600;
PB_Bag.GetAllItemReqMsg = function(archive, archive_type)
    local data = pb.encode(archive_type, archive)
    return data
end

return PB_Bag
